﻿using AutoMapper.Internal.Mappers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Himp.Platform
{
    /// <summary>
    /// 
    /// </summary>
    public class AttrDictService : CrudAppService<AttrDict, AttrDictDto, Guid, AttrDictInput, AttrDictCreateDto, AttrDictUpdateDto>
    {
        private IRepository<AttrDict, Guid> _attrDictRepository;

        public AttrDictService(IRepository<AttrDict, Guid> attrDictRepository) : base(attrDictRepository)
        {
            _attrDictRepository = attrDictRepository;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public override async Task<PagedResultDto<AttrDictDto>> GetListAsync(AttrDictInput input)
        {
            var iQuery = await _attrDictRepository.WithDetailsAsync();

            if (input.Pi == 0)
            {
                var itemsDto = ObjectMapper.Map<List<AttrDict>, List<AttrDictDto>>(iQuery.ToList());

                return new PagedResultDto<AttrDictDto>(itemsDto.Count, itemsDto);
            }
            else
            {
                var totalCount = iQuery.Count();
                var items = iQuery.Page(input.Pi, input.Ps);
                var itemsDto = ObjectMapper.Map<List<AttrDict>, List<AttrDictDto>>(items.ToList());

                return new PagedResultDto<AttrDictDto>(totalCount, itemsDto);
            }
        }
    }
}
